﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Module.Library;
using Csla.OpenExpressApp;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.MetaModel;
using OpenExpressApp.Types;
using OpenExpressApp.WPF.Command;
using OpenExpressApp.WPF;
using OpenExpressApp.Template;
using System.ComponentModel;
using Avalon.Windows.Controls;
using System.Windows.Controls;
using System.Windows;
using System.Diagnostics;
using Itenso.Windows.Input;

namespace OpenExpressApp.Module.WPF.Command
{

    [Command(CommandNames.CancleCheckOperationCommand, ToolbarType = ToolbarType.Child, TargetObjectType = typeof(OrgPositionOperation), Label = "取消全部功能", ToolTip = "取消全部功能")]
    public class CancleCheckOperationCommand : WPFListViewCommand
    {
        public override bool CanExecute(ListObjectView view)
        {
            return (view.Data != null);
        }

        public override void Execute(ListObjectView view)
        {
            var naviView = view.NavigateQueryView;
            var allOperations = view.UnderlyData as OrgPositionOperations;

            var list = (naviView.GetChildView(typeof(BusinessObjectInfo), true) as ListObjectView).SelectedObjects;

            if (list.Count > 0)
            {
                var criteria = naviView.CurrentObject as OrgPositionOperationCriteria;
                var selectedList = criteria.SelectedBOs;
                selectedList.Clear();
                foreach (BusinessObjectInfo item in list)
                {
                    selectedList.Add(item);
                }

                var allDisplayModels = allOperations.GetDisplayModels(criteria as IQueryObject);

                foreach (BoInfoOperationSelected dm in allDisplayModels)
                {
                    if (string.Compare(dm.OperationId.ToString(), CommandNames.CanViewObject.ToString(), true) == 0)
                    {
                        dm.IsSelected = false;
                    }
                }

                if (criteria.BusinessObjectInfo != null)
                {
                    selectedList.Clear();
                    selectedList.Add(criteria.BusinessObjectInfo);
                    naviView.ExecuteQuery();
                }
            }
        }
    }
}
